package com.hydl.power.utils;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class ElectricityFeesUtils {


    public static void main(String[] args) {
        double averagePowerFactor = getAveragePowerFactor(16458.00, 3.00);
        double electricityFees = getElectricityFees(averagePowerFactor, 0.585, 16458.00);
        System.out.println(electricityFees);


    }


    /**
     * 计算平均功率因数
     * @param activeEnergy 有功电能
     * @param ReactiveEnergy 无功电能
     * @return  平均功率因数
     */
    public static double getAveragePowerFactor(double activeEnergy,double ReactiveEnergy){
        //计算功率因数    公式:  有功电量/根号(有功电能平方+ 无功电能平方)
        double v = activeEnergy/(Math.sqrt((Math.pow(activeEnergy, 2) + Math.pow(ReactiveEnergy, 2))));
        //保留两位小数
        DecimalFormat formater = new DecimalFormat();
        formater.setMaximumFractionDigits(2);
        formater.setRoundingMode(RoundingMode.FLOOR);
        double averagePowerFactor =Double.valueOf(formater.format(v));
        return averagePowerFactor;
    }



    /**
     * 计算力调电费
     * @param averagePowerFactor 平均功率因数
     * @param price  电费单价
     * @param electricityConsumption  用电量
     * @return 奖罚金额
     */
    public static double getElectricityFees(double averagePowerFactor , double price , double electricityConsumption){
        double electricityFees = electricityConsumption * price;
        if (0.9>averagePowerFactor && averagePowerFactor>=0.7){
            int count = (int)(new Float(0.9 - averagePowerFactor)* 100);
            double v =  (count * 0.005 * electricityFees)*-1;
            return v;
        }else if(0.7>averagePowerFactor && averagePowerFactor >=0.65){
            int count = (int)(new Float(0.7 - averagePowerFactor)* 100);
            double v = ((count * 0.005 * electricityFees)+( 20*0.005 * electricityFees))*-1;
            return v;
        }else if(averagePowerFactor<0.65){
            int count = (int)(new Float(0.65 - averagePowerFactor)* 100);
            double v = ((count * 0.02 * electricityFees)+( 5*0.01 * electricityFees)+( 20*0.005 * electricityFees))*-1;
            return v;
        }else if(0.95>averagePowerFactor &&  averagePowerFactor>0.9){
            int count = (int)(new Float(0.95 - averagePowerFactor)* 100);
            double v = count * 0.0015 * electricityFees;
            return v;
        }else if(averagePowerFactor>=0.95){
            double v = electricityFees * 0.0075;
            return v;
        }
        return 0;
    }






}
